package com.example.xunai.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRabbit
public class RabbitMQConfig {

    // AI 处理队列
    public static final String AI_PROCESS_QUEUE = "ai.process.queue";
    public static final String AI_PROCESS_EXCHANGE = "ai.process.exchange";
    public static final String AI_PROCESS_ROUTING_KEY = "ai.process";

    // AI 批量处理队列
    public static final String AI_BATCH_PROCESS_QUEUE = "ai.batch.process.queue";
    public static final String AI_BATCH_PROCESS_EXCHANGE = "ai.batch.process.exchange";
    public static final String AI_BATCH_PROCESS_ROUTING_KEY = "ai.batch.process";

    // 消息保存队列
    public static final String MESSAGE_SAVE_QUEUE = "message.save.queue";
    public static final String MESSAGE_SAVE_EXCHANGE = "message.save.exchange";
    public static final String MESSAGE_SAVE_ROUTING_KEY = "message.save";

    // AI 重试队列
    public static final String AI_RETRY_QUEUE = "ai.retry.queue";
    public static final String AI_RETRY_EXCHANGE = "ai.retry.exchange";
    public static final String AI_RETRY_ROUTING_KEY = "ai.retry";

    // AI 批量重试队列
    public static final String AI_BATCH_RETRY_QUEUE = "ai.batch.retry.queue";
    public static final String AI_BATCH_RETRY_EXCHANGE = "ai.batch.retry.exchange";
    public static final String AI_BATCH_RETRY_ROUTING_KEY = "ai.batch.retry";

    @Bean
    public Queue aiProcessQueue() {
        return new Queue(AI_PROCESS_QUEUE, true);
    }

    @Bean
    public DirectExchange aiProcessExchange() {
        return new DirectExchange(AI_PROCESS_EXCHANGE);
    }

    @Bean
    public Binding aiProcessBinding() {
        return BindingBuilder.bind(aiProcessQueue())
                .to(aiProcessExchange())
                .with(AI_PROCESS_ROUTING_KEY);
    }

    @Bean
    public Queue aiBatchProcessQueue() {
        return QueueBuilder.durable(AI_BATCH_PROCESS_QUEUE)
                .build();
    }

    @Bean
    public DirectExchange aiBatchProcessExchange() {
        return new DirectExchange(AI_BATCH_PROCESS_EXCHANGE);
    }

    @Bean
    public Binding aiBatchProcessBinding() {
        return BindingBuilder.bind(aiBatchProcessQueue())
                .to(aiBatchProcessExchange())
                .with(AI_BATCH_PROCESS_ROUTING_KEY);
    }

    @Bean
    public Queue messageSaveQueue() {
        return new Queue(MESSAGE_SAVE_QUEUE, true);
    }

    @Bean
    public DirectExchange messageSaveExchange() {
        return new DirectExchange(MESSAGE_SAVE_EXCHANGE);
    }

    @Bean
    public Binding messageSaveBinding() {
        return BindingBuilder.bind(messageSaveQueue())
                .to(messageSaveExchange())
                .with(MESSAGE_SAVE_ROUTING_KEY);
    }

    @Bean
    public Queue aiRetryQueue() {
        return QueueBuilder.durable(AI_RETRY_QUEUE)
                .deadLetterExchange(AI_PROCESS_EXCHANGE)
                .deadLetterRoutingKey(AI_PROCESS_ROUTING_KEY)
                .ttl(30000) // 30秒后重试
                .build();
    }

    @Bean
    public DirectExchange aiRetryExchange() {
        return new DirectExchange(AI_RETRY_EXCHANGE);
    }

    @Bean
    public Binding aiRetryBinding() {
        return BindingBuilder.bind(aiRetryQueue())
                .to(aiRetryExchange())
                .with(AI_RETRY_ROUTING_KEY);
    }

    @Bean
    public Queue aiBatchRetryQueue() {
        return QueueBuilder.durable(AI_BATCH_RETRY_QUEUE)
                .deadLetterExchange(AI_BATCH_PROCESS_EXCHANGE)
                .deadLetterRoutingKey(AI_BATCH_PROCESS_ROUTING_KEY)
                .ttl(60000) // 批量处理重试时间稍长，60秒
                .build();
    }

    @Bean
    public DirectExchange aiBatchRetryExchange() {
        return new DirectExchange(AI_BATCH_RETRY_EXCHANGE);
    }

    @Bean
    public Binding aiBatchRetryBinding() {
        return BindingBuilder.bind(aiBatchRetryQueue())
                .to(aiBatchRetryExchange())
                .with(AI_BATCH_RETRY_ROUTING_KEY);
    }

    @Bean
    public Jackson2JsonMessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}